package xcmg.device.service.sale;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.salesReturn.SalesReturnApplyDetailDO;
import xcmg.device.dao.entity.salesReturn.SalesReturnInDO;
import xcmg.device.dao.entity.salesReturn.SalesReturnInDetailDO;
import xcmg.device.dao.mapper.MaterialLocationRelationMapper;
import xcmg.device.dao.mapper.salesReturn.ReturnApplyDetailMapper;
import xcmg.device.dao.mapper.salesReturn.ReturnInDetailMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.basic.MaterialLocationRelationVO;
import xcmg.device.service.vo.salesReturn.SalesReturnInDetailVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 退货入库单
 * Created by LiChangjiang on 2018/3/6 0006.
 */
@Service
public class ReturnInDetailService
{
    @Autowired
    private ReturnInDetailMapper returnInDetailMapper;

    @Autowired
    private ReturnApplyDetailMapper returnApplyDetailMapper;

    @Autowired
    private MaterialLocationRelationMapper materialLocationRelationMapper;

    @Autowired
    private FastGenClient fastGenClient;

    /**
     * 查询退货入库单的零件信息列表
     *
     * @param returnNo String
     * @return List
     */
    public List<SalesReturnInDetailVO> list(String returnNo, SalesReturnInDO salesReturnInDO)
    {
         List<SalesReturnInDetailDO> salesReturnInDetailDOs = returnInDetailMapper.listForLocationUser(returnNo);

        List<SalesReturnInDetailVO> resultList = new ArrayList<>();

        if (ListUtil.isNullOrEmpty(salesReturnInDetailDOs))
        {
            return resultList;
        }

        MaterialLocationRelationVO relationVO = new MaterialLocationRelationVO();
        MaterialLocationRelationVO materialLocationRelationVO = new MaterialLocationRelationVO();
        for (SalesReturnInDetailDO record : salesReturnInDetailDOs)
        {
            SalesReturnInDetailVO returnInDetailVO = getVO(record);
            relationVO.setOrgId(salesReturnInDO.getOrgId());
            relationVO.setMaterialNo(record.getMaterialsNo());
            relationVO.setWarehouseCode(salesReturnInDO.getWarehouseCode());
            relationVO.setAreaCode(salesReturnInDO.getInArea());
            returnInDetailVO.setStockLocation(materialLocationRelationMapper.findLocationCode(relationVO));
            materialLocationRelationVO.setMaterialNo(record.getMaterialsNo());
            materialLocationRelationVO.setOrgId(salesReturnInDO.getOrgId());
            returnInDetailVO.setStockLocationList(materialLocationRelationMapper.list(materialLocationRelationVO));
            resultList.add(returnInDetailVO);
        }
        return resultList;
    }

    /**
     * 查询零件详细
     * @param id
     * @return
     */
    public SalesReturnInDetailVO item(String id)
    {
        SalesReturnInDetailDO salesReturnInDetailDO = returnInDetailMapper.itemById(id);
        return getVO(salesReturnInDetailDO);
    }

    /**
     * 添加退货入库单中零件信息
     *
     * @param salesReturnInDetailVOs List
     * @param returnNo               String
     * @return ErrorCode
     */
    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(List<SalesReturnInDetailVO> salesReturnInDetailVOs, String returnNo) throws Exception
    {
        for (SalesReturnInDetailVO inDetailVO : salesReturnInDetailVOs)
        {
            ActionResult<String> textResult = fastGenClient.textGuid();
            if (textResult == null || textResult.getCode() != 0)
            {
                return ErrorCode.Failure;
            }
            SalesReturnInDetailDO inDetailDO = getDO(inDetailVO);
            inDetailDO.setId(String.valueOf(textResult.getValue()));
            inDetailDO.setReturnNo(returnNo);
            int addRet = returnInDetailMapper.insert(inDetailDO);

            if (0 >= addRet)
            {
                throw new Exception("添加退货入库单零件信息异常！");
            }
        }
        return ErrorCode.Success;
    }

    /**
     * @param beanDO SalesReturnApplyDetailDO
     * @return SalesReturnApplyDetailVO
     */
    private SalesReturnInDetailVO getVO(SalesReturnInDetailDO beanDO)
    {
        if (null == beanDO)
        {
            return null;
        }
        SalesReturnInDetailVO beanVO = new SalesReturnInDetailVO();
        BeanUtils.copyProperties(beanDO, beanVO);
        SalesReturnApplyDetailDO applyDetailDO = returnApplyDetailMapper.itemById(beanDO.getApplyDetailId());
        if(null!=applyDetailDO)
        {
            beanVO.setApplyQty(applyDetailDO.getApplyQty());
            beanVO.setReturnQty(applyDetailDO.getReturnQty());
        }

        //0待审核，1审核拒绝，2审核通过
        if (1 == beanDO.getStatus())
        {
            beanVO.setStatusDesc("部分完成");
        }
        else if (2 == beanDO.getStatus())
        {
            beanVO.setStatusDesc("完成");
        }
        else
        {
            beanVO.setStatusDesc("未完成");
        }
        return beanVO;
    }

    /**
     * @param beanVO SalesReturnApplyDetailVO
     * @return SalesReturnApplyDetailDO
     */
    private SalesReturnInDetailDO getDO(SalesReturnInDetailVO beanVO)
    {
        if (null == beanVO)
        {
            return null;
        }
        SalesReturnInDetailDO beanDO = new SalesReturnInDetailDO();

        BeanUtils.copyProperties(beanVO, beanDO);
        return beanDO;
    }

}
